package cn.org.intelli.lims.o2oa.model;

import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.SQLDelete;
import xyz.erupt.annotation.Erupt;
import xyz.erupt.annotation.EruptField;
import xyz.erupt.annotation.fun.DataProxy;
import xyz.erupt.annotation.sub_erupt.Filter;
import xyz.erupt.annotation.sub_field.Edit;
import xyz.erupt.annotation.sub_field.EditType;
import xyz.erupt.annotation.sub_field.View;
import xyz.erupt.annotation.sub_field.ViewType;
import xyz.erupt.annotation.sub_field.sub_edit.AttachmentType;
import xyz.erupt.annotation.sub_field.sub_edit.Search;

import javax.persistence.*;
import java.util.Date;
import java.util.Set;
import java.util.UUID;

@Getter
@Setter
@Entity
@Table(name = "lims_document")
@SQLDelete(sql = "update lims_document set deleted = true, delete_time = now() where id = ?")
@Erupt(name = "文件信息", dataProxy = Document.class, filter = @Filter("Document.deleted = false"))
public class Document implements DataProxy<Document> {
    @Id
    @EruptField(views = @View(title = "", type = ViewType.QR_CODE))
    private String id;
    @EruptField(views = @View(title = "文件名"), edit = @Edit(title = "文件名", search = @Search))
    private String name;
    @EruptField(views = @View(title = "文件"), edit = @Edit(title = "文件上传", type = EditType.ATTACHMENT, attachmentType = @AttachmentType))
    private String attachment;
    @ManyToMany
    @JoinTable(name = "lims_document_document_tag", joinColumns = @JoinColumn(name = "document_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "document_tag_id", referencedColumnName = "id"))
    @EruptField(edit = @Edit(title = "文件标签信息", type = EditType.TAB_TABLE_REFER))
    private Set<DocumentTag> tags;
    @Column(name = "delete_time")
    private Date deleteTime;
    private Boolean deleted = false;

    @Override
    public void beforeAdd(Document document) {
        if (document.getId() == null) {
            document.setId(UUID.randomUUID().toString());
        }
    }
}
